package _05_二叉树;

import java.util.LinkedList;

public class _104_二叉树的最大深度 {

    // 递归
    public int maxDepth1(TreeNode root) {
        if (root == null) return 0;

        return 1 + Math.max(maxDepth1(root.left), maxDepth1(root.right));
    }

    // 迭代
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int height = 0;
        int levelSize = 1;
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();

            if (node.left != null) queue.offer(node.left);

            if (node.right != null) queue.offer(node.right);

            if (--levelSize == 0) {
                levelSize = queue.size();
                height++;
            }
        }
        return height;
    }


}
